using System;
using System.IO;
using TDUnit.Common.Logging;

namespace TDUnit.Loggers
{
    public class StreamLogger : ILogger
    {
        private readonly StreamWriter logWriter;

        public StreamLogger(Stream logStream)
        {
            if(logStream == null)
                throw new ArgumentNullException("logStream");
                
            if(logStream.CanWrite == false)
                throw new LoggingException("Stream being used for logging must be writtable.");

            logWriter = new StreamWriter(logStream);
        }

        public void LogMessage(string message, MessageType type)
        {
            var date = DateTime.Now;
            logWriter.WriteLine(string.Format("{0}: ({1}) {2}", type, date.ToString("yyyyMMddhhmmss"), message));
            logWriter.Flush();
        }

        public void Dispose()
        {
            logWriter.Dispose();
        }
    }
}